package com.symone.crawler.data;

import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@Data
public class EntityMapper implements Serializable {

    private String tableName;
    private List<String> fields;
    private List<String> pks;

    public static EntityMapper create() {
        return new EntityMapper();
    }

    public static EntityMapper create(String tableName) {
        return new EntityMapper(tableName);
    }

    public EntityMapper() {
        this.fields = new ArrayList<>();
        this.pks = new ArrayList<>();
    }

    public EntityMapper(String tableName) {
        this.tableName = tableName;
        this.fields = new ArrayList<>();
        this.pks = new ArrayList<>();
    }

    public EntityMapper(String tableName,String fields,String pks){
        this.tableName = tableName;
        this.fields = new ArrayList<>();
        this.pks = new ArrayList<>();
        String[] fieldArray = fields.split(",");
        for(String field : fieldArray){
            this.fields.add(field.trim());
        }
        String[] pkArray = pks.split(",");
        for(String pk : pkArray){
            this.pks.add(pk.trim());
        }
    }

    public EntityMapper setFields(String fields){
        String[] fieldArray = fields.split(",");
        for(String field : fieldArray){
            this.fields.add(field.trim());
        }
        return this;
    }

    public EntityMapper setFields(String... fields){
        for(String field : fields){
            this.fields.add(field.trim());
        }
        return this;
    }

    public EntityMapper setPks(String pks){
        String[] pkArray = pks.split(",");
        for(String pk : pkArray){
            this.pks.add(pk.trim());
        }
        return this;
    }

    public EntityMapper setPks(String... pks){
        for(String pk : pks){
            this.pks.add(pk.trim());
        }
        return this;
    }

}
